#!/usr/bin/env node

const log = console.log

//Buffer = Uint8Array
//本质：字节数组
//功能：1.编码 2.读写二进制数据
let msg = 'hello 王顶'

//string =>  encoding

let utf8 = Buffer.from(msg, 'utf8')
let utf16 = Buffer.from(msg, 'utf16le')
log(utf8)
log(utf16)



const buf1 = Buffer.alloc(256)
buf1[0] = 0
log('buf1 length:', buf1.length)
log('buf1:', buf1)

for (let i = 0; i < buf1.length; i++) {
    buf1[i] = i
}
log('buf1:', buf1)

let buf2 = buf1.slice(buf1.length - 10, buf1.length)
log('buf2 length:', buf2.length)
log('buf2:', buf2)

buf2.fill(0)
log(buf2)

let array = ['a', 0xba, 0xdf, 255, 10]
let buf3 = Buffer.from(array)
log(buf3.length, buf3)

let buf4 = Buffer.from('hello, world', 'utf8')
log(buf4, buf4.length, buf4.toString())

buf3 = Buffer.alloc(buf4.length)
buf4.copy(buf3, 0, 0, buf4.length)
log(buf3, buf3.length, buf3.toString())
